﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PLM.ConfigurationCenter.Core.Util;
using PLM.ConfigurationCenter.Model;
using PLM.ConfigurationCenter.Service;
using static PLM.ConfigurationCenter.Model.Extensions;

namespace PLM.ConfigurationCenter.Backup.CustomForms {
    public partial class SettingForm : Form {

        /// <summary>
        /// 
        /// </summary>
        private SettingService _settingService = new SettingService();
        private SettingModel SettingModel;
        /// <summary>
        /// 
        /// </summary>
        public SettingForm() {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SettingForm_Load(object sender, EventArgs e) {
            SetSetting();
            gbxEmail.Enabled = rbtnEmailYes.Checked;
            gbxLog.Enabled = rbtnLogYes.Checked;
            cbxDel.Enabled = rbtnMessAndData.Checked;
            if (rbtnMess.Checked)
                cbxDel.Checked = false;
        }
        /// <summary>
        /// 
        /// </summary>
        private void SetSetting() {
            SettingModel = _settingService.GetSetting() ?? new SettingModel();
            rbtnEmailYes.Checked = SettingModel.IsMail;
            rbtnEmailNo.Checked = !SettingModel.IsMail;
            tbxEmailHost.Text = SettingModel.EmailHost;
            tbxEmailPort.Text = SettingModel.EmailPort;
            tbxSendEmail.Text = SettingModel.SendEmail;
            tbxPassword.Text = SettingModel.Password;
            tbxReceivedEmail.Text = SettingModel.ReceivedEmail;
            rbtnMess.Checked = SettingModel.EmailType == EmailType.通知;
            rbtnMessAndData.Checked = SettingModel.EmailType != EmailType.通知;
            rbtnLogYes.Checked = SettingModel.IsLogs;
            rbtnLogNo.Checked = !SettingModel.IsLogs;
            tbxLogPath.Text = SettingModel.LogsPath;
            cbxDel.Checked = SettingModel.IsDel;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogChoose_Click(object sender, EventArgs e) {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK) {
                tbxLogPath.Text = dialog.SelectedPath;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbtnEmailYes_CheckedChanged(object sender, EventArgs e) {
            gbxEmail.Enabled = rbtnEmailYes.Checked;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbtnLogYes_CheckedChanged(object sender, EventArgs e) {
            gbxLog.Enabled = rbtnLogYes.Checked;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rbtnMess_CheckedChanged(object sender, EventArgs e) {
            cbxDel.Enabled = !rbtnMess.Checked;
            if (rbtnMess.Checked)
                cbxDel.Checked = false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTestEmail_Click(object sender, EventArgs e) {
            if (!Checked()) return;
            btnTestEmail.Text = @"发送中...";
            btnTestEmail.Enabled = false;
            var ok = CommonHandler.SendMail(int.Parse(tbxEmailPort.Text), tbxEmailHost.Text, tbxSendEmail.Text, tbxReceivedEmail.Text, tbxPassword.Text,
                "Oracle数据库备份邮件接收测试",
                "用户您好：当您看到这封邮件时说明您的邮件接收配置已经成功！");
            btnTestEmail.Text = @"测试发送";
            btnTestEmail.Enabled = true;
            if (ok) {
                MessageBox.Show(@"发送成功，请到邮箱查看！", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } else {
                MessageBox.Show(@"发送失败，请认真检查邮箱配置！", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private bool Checked() {
            if (rbtnEmailYes.Checked) {
                if (string.IsNullOrEmpty(tbxEmailHost.Text.Trim())) {
                    MessageBox.Show(@"服务地址不可为空", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
                if (string.IsNullOrEmpty(tbxEmailPort.Text.Trim())) {
                    MessageBox.Show(@"端口不可为空", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
                if (string.IsNullOrEmpty(tbxSendEmail.Text.Trim())) {
                    MessageBox.Show(@"发送邮箱不可为空", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
                if (string.IsNullOrEmpty(tbxPassword.Text.Trim())) {
                    MessageBox.Show(@"密码不可为空", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
                if (string.IsNullOrEmpty(tbxReceivedEmail.Text.Trim())) {
                    MessageBox.Show(@"接收邮箱不可为空", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }

            if (rbtnLogYes.Checked) {
                if (string.IsNullOrEmpty(tbxLogPath.Text.Trim())) {
                    MessageBox.Show(@"存储路径不可为空", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }

            return true;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSave_Click(object sender, EventArgs e) {
            if (!Checked()) return;

            SettingModel.IsMail = rbtnEmailYes.Checked;
            SettingModel.EmailHost = tbxEmailHost.Text.Trim();
            SettingModel.EmailPort = tbxEmailPort.Text.Trim();
            SettingModel.SendEmail = tbxSendEmail.Text.Trim();
            SettingModel.Password = tbxPassword.Text.Trim();
            SettingModel.ReceivedEmail = tbxReceivedEmail.Text.Trim();
            SettingModel.EmailType = rbtnMess.Checked ? EmailType.通知 : EmailType.通知和数据;
            SettingModel.IsLogs = rbtnLogYes.Checked;
            SettingModel.LogsPath = tbxLogPath.Text.Trim();
            SettingModel.IsDel = cbxDel.Checked;

            //删除 原有数据
            _settingService.DeleteSetting();
            //保存配置
            SettingModel.Id = Guid.NewGuid().ToString("N");
            _settingService.Insert(SettingModel);


            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }
    }
}
